package com.tingyuge.thinking.spring.configuration.metadata;

import com.tingyuge.thinking.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import java.util.Map;

/**
 * 将当前类作为configuration Class
 * //1.有一个类的bean,是因为AnnotationConfigApplicationContext.annotatedNameGenerator 根据类名显示
 *
 * @author xiaodao
 * @version 1.0
 * @date 2021/7/13
 */
@ImportResource("classpath:/META-INF/dependency-lookup-context.xml")
@Import(User.class)
@PropertySource("classpath:/META-INF/user-bean-definitions.properties") //俩个注解基于java 8
@PropertySource("classpath:/META-INF/user-bean-definitions.properties")
//@PropertySources({@PropertySource("classpath:/META-INF/user-bean-definitions.properties")})
public class AnnotatedSpringIoCContainerMetadataConfigurationDemo {

    /**
     * user.name 是 java properties默认存在,当前用户就是xiaodao,而不是配置中定义的 '听雨阁'
     * @param id
     * @param name
     * @return
     */
    @Bean
    public User configuredUser(@Value("${user.id}" ) Long id,@Value("${user.name}" )String name){
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }

    public static void main(String[] args) {


        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册当前类作为Configuration class
        applicationContext.register(AnnotatedSpringIoCContainerMetadataConfigurationDemo.class);
        applicationContext.refresh();
        Map<String, User> userMap = applicationContext.getBeansOfType(User.class);

        for (Map.Entry<String, User> stringUserEntry : userMap.entrySet()) {
            System.out.printf(" User bean name %s  value: %s \n", stringUserEntry.getKey(), stringUserEntry.getValue());
        }
        applicationContext.close();
    }
}
